<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :showOkBtn="false"
    :okAuth="'process:analysis:pmWateranalysis:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="50%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
    <template #footer>
      <BpmButton
        v-model:bpmEntity="record"
        bpmEntityKey="id"
        formKey="Flow_PmWaterAnalysis"
        completeText="提交"
        :loading="loadingRef"
         :auth="'process:analysis:pmWateranalysis:edit'"
        @complete="handleSubmit"
        @success="handleSuccess"
        @close="closeDrawer"
        :onlyShow="true"
      />
    </template>
    <!-- <template #appendFooter>
      <a-button type="primary" @click="handleTrace()" v-if="record.status != '9'">
        <Icon icon="fluent:flowchart-20-regular" /> {{ t('流程图') }}
      </a-button>
    </template> -->
    <BpmRuntimeTrace @register="registerTraceModal" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProcessAnalysisPmWateranalysisForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { PmWateranalysis, pmWateranalysisSave, pmWateranalysisForm } from '/@/api/process/analysis/pmWateranalysis';
  import { pmProjectinformationTreeData } from '/@/api/proj/info/pmProjectinformation';
  import { BpmRuntimeTrace,BpmButton } from '/@/components/Bpm';
  import { useModal } from '/@/components/Modal';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('process.analysis.pmWateranalysis');
  const { showMessage } = useMessage();
  const record = ref<PmWateranalysis>({} as PmWateranalysis);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增水质检测') : t('编辑水质检测'),
  }));

  const loadingRef = ref(false);

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('name'),
      field: 'name',
      component: 'Input',
      componentProps: {
        maxlength: 500,
      },
      ifShow: false,
    },
    {
      label: t('申请信息'),
      field: 'projectInfo',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('工程名称'),
      field: 'projectno',
      fieldLabel: 'projectname',
      component: 'TreeSelect',
      componentProps: {
        api: pmProjectinformationTreeData,
        params: {},
        canSelectParent: false,
        allowClear: false,
        disabled: true,
      },
      itemProps: { validateTrigger: 'blur' },
      required: true,
    },
    {
      label: t('工程编码'),
      field: 'projectno',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('施工单位'),
      field: 'constructionunit',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('联系人'),
      field: 'contacts',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('联系电话'),
      field: 'telephone',
      component: 'Input',
      componentProps: {
        maxlength: 50,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('采样地址'),
      field: 'address',
      component: 'Input',
      componentProps: {
        maxlength: 800,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('申请说明'),
      field: 'remarks',
      component: 'Input',
      componentProps: {
        maxlength: 500,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('申请附件'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmWaterAnalysis_file',
        uploadType: 'all',
        disabled: true,
      },
      colProps: { lg: 24, md: 24 },
      // required: true,
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['testData_image__len'] || 0;
      //         if (len == 0) reject(t('请上传附件'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
    },
    {
      label: t('结果信息'),
      field: 'projectResult',
      component: 'FormGroup',
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('检测报告评价'),
      field: 'assayassay',
      component: 'InputTextArea',
      componentProps: {
        maxlength: -1,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('检测结果'),
      field: 'assayresult',
      component: 'RadioGroup',
      componentProps: {
        dictType: 'zf_assay_result',
        allowClear: true,
        disabled: true,
      },
      required: true,
    },
    {
      label: t('检测结果附件'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'PmWaterAnalysis_rs_file',
        uploadType: 'all',
        disabled: true,
      },
      colProps: { lg: 24, md: 24 },
      // required: true,
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['testData_image__len'] || 0;
      //         if (len == 0) reject(t('请上传文件'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
    },
    {
      label: t('申请日期'),
      field: 'applydate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      ifShow: false,
    },
    {
      label: t('applypic'),
      field: 'applypic',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      ifShow: false,
    },
    {
      label: t('检测上传时间'),
      field: 'assayupdate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm',
        showTime: { format: 'HH:mm' },
      },
      ifShow: false,
    },
    {
      label: t('assayfile'),
      field: 'assayfile',
      component: 'Input',
      componentProps: {
        maxlength: -1,
      },
      ifShow: false,
    },

    {
      label: t('ret'),
      field: 'ret',
      component: 'Input',
      componentProps: {
        maxlength: 50,
      },
      ifShow: false,
    },
    {
      label: t('图片上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'pmWateranalysis_image',
        uploadType: 'image',
      },
      colProps: { lg: 24, md: 24 },
      ifShow: false,
    },
    {
      label: t('附件上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'pmWateranalysis_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
      ifShow: false,
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await pmWateranalysisForm(data);
    record.value = (res.pmWateranalysis || {}) as PmWateranalysis;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    setDrawerProps({ loading: false });
  });

  const [registerTraceModal, { openModal: traceModel }] = useModal();
  function handleTrace() {
    traceModel(true, { formKey: 'Flow_PmWaterAnalysis', bizKey: record.value.id });
  }

  async function handleSubmit() {
    try {
      const data = await validate();
      data.bpm = record.value.bpm; // 流程信息
      data.status = record.value.status; // 提交状态
      loadingRef.value = true;
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await pmWateranalysisSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      loadingRef.value = false;
      setDrawerProps({ confirmLoading: false });
    }
  }

  async function handleSuccess() {
    emit('success');
  }
</script>
